package cn.tedu.service.customer.impl;

import cn.tedu.commons.exception.ServiceException;
import cn.tedu.commons.response.JsonResult;
import cn.tedu.commons.response.ServiceCode;
import cn.tedu.commons.util.GlobalPasswordEncoder;
import cn.tedu.mapper.customer.CustomerLoginMapper;
import cn.tedu.pojo.customer.dto.CustomerLoginAddNewDTO;
import cn.tedu.pojo.customer.dto.CustomerLoginDTO;
import cn.tedu.pojo.customer.entity.CustomerLogin;
import cn.tedu.pojo.customer.vo.CustomerLoginVO;
import cn.tedu.service.customer.ICustomerLoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
public class CustomerLoginServiceImpl implements ICustomerLoginService {

    @Autowired
    private CustomerLoginMapper customerLoginMapper;

    @Autowired
    private GlobalPasswordEncoder globalPasswordEncoder;


    @Override
    public void addNew(CustomerLoginAddNewDTO customerAddrAddNewDTO) {
        String username =customerAddrAddNewDTO.getUserName();

        CustomerLogin queryResult =customerLoginMapper.getByUserName(username);
        System.out.println(queryResult);
        if (queryResult !=null){
            //用户名已经存在，增加失败，抛出异常
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"用户名已经存在，注册失败");
        }
        //以参数CustomerLoginAddNewDTO中的password作为明文，执行加密，得到密文密码
        String rawPassword =customerAddrAddNewDTO.getUserPassword();
        String encodePassword = globalPasswordEncoder.encode(rawPassword);
        log.debug("对密码进行加密处理，原文={}，密文={}",rawPassword,encodePassword);
        //创建新的Customer对象
        CustomerLogin customerLogin = new CustomerLogin();
        customerLogin.setUserName(username);
        customerLogin.setUserPassword(encodePassword);
        customerLogin.setUserPicture("luban");
        customerLogin.setIsDelete(0);
        customerLogin.setUserStats(0);
        LocalDateTime now =LocalDateTime.now();
        customerLogin.setGmtCreate(now);
        customerLogin.setGmtModified(now);
        log.debug("即将插入用户数据:{}",customerLogin);
        int rows = customerLoginMapper.insert(customerLogin);
        if (rows!=1){
            //抛出异常
            log.warn("服务器忙，请稍后再次尝试！");
            throw  new ServiceException(ServiceCode.INTERNAL_SERVER_ERROR,"服务器忙，请稍后再试");
        }
    }

    /**
     * 根据用户id查询用户
     *
     * */
    @Override
    public CustomerLogin getById(Long id) {
        CustomerLogin customer = customerLoginMapper.getById(id);

        if (customer==null){
            //不存在该用户
             throw new ServiceException(ServiceCode.NOT_FOUND, "用户名不存在！");
        }
       return customer;
    }

    @Override
    public CustomerLogin getByUserName(String userName) {
       CustomerLogin customerLogin = customerLoginMapper.getByUserName(userName);

        if (customerLogin==null){
            //不存在该用户
            throw new ServiceException(ServiceCode.NOT_FOUND, "用户名不存在！");
        }
        return customerLogin;
    }

    @Override
    public CustomerLoginDTO login(CustomerLoginDTO customerLoginDTO) {
        //获取用户输入的用户名 username
        CustomerLogin userName = customerLoginMapper.getByUserName(customerLoginDTO.getUserName());
       //比较用户名是否存在
        if (userName == null){
            throw new ServiceException(ServiceCode.NOT_FOUND,"用户名不存在，请注册");

            /*跳转注册链接*/

        }else {
            //进行密码的比对
            //获取用户输入的密码，进行加密
            String rawPassword =customerLoginDTO.getUsePassword();
            String encodePassword1 = globalPasswordEncoder.encode(rawPassword);
            //用username 找到数据库中的密码
            CustomerLogin encodePassword2 = customerLoginMapper.getPassWordByUserName(customerLoginDTO.getUserName());
            if (encodePassword2.equals(encodePassword1)){

            }
        }

        return null;
    }

    @Override
    public CustomerLogin getPasswordByUserName(String userName) {
        CustomerLogin encodePassword  = customerLoginMapper.getPassWordByUserName(userName);
        return encodePassword;
    }

    @Override
    public CustomerLogin getIdByUserName(String userName) {
        CustomerLogin id = customerLoginMapper.getIdByUserName(userName);
        return id;
    }

    @Override
    public CustomerLogin getUserNameById(Long id) {
        CustomerLogin userName = customerLoginMapper.getUserNameById(id);
        return userName;
    }

    @Override
    public CustomerLoginVO getByDelete(String userName, Integer isDelete) {
        return null;
    }

    @Override
    public CustomerLogin updateDelete(String userName, Integer isDelete) {
        CustomerLogin customerLogin = customerLoginMapper.getByUserName(userName);
        if (customerLogin==null){
            throw new ServiceException(ServiceCode.NOT_FOUND,"该用户不存在");
        }
        CustomerLoginVO customerLoginVO = customerLoginMapper.updateDelete(userName, isDelete);
        return null;
    }







}

